package cn.iocoder.yudao.module.famous.doctor.service.dividegroups;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.dividegroups.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.dividegroups.DividegroupsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.dividegroups.DividegroupsConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.dividegroups.DividegroupsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 我的分组 Service 实现类
 *
 * @author 启航
 */
@Service
@Validated
public class DividegroupsServiceImpl implements DividegroupsService {

    @Resource
    private DividegroupsMapper dividegroupsMapper;

    @Override
    public Long createDividegroups(DividegroupsCreateReqVO createReqVO) {
        // 插入
        DividegroupsDO dividegroups = DividegroupsConvert.INSTANCE.convert(createReqVO);
        dividegroupsMapper.insert(dividegroups);
        // 返回
        return dividegroups.getId();
    }

    @Override
    public void updateDividegroups(DividegroupsUpdateReqVO updateReqVO) {
        // 校验存在
        validateDividegroupsExists(updateReqVO.getId());
        // 更新
        DividegroupsDO updateObj = DividegroupsConvert.INSTANCE.convert(updateReqVO);
        dividegroupsMapper.updateById(updateObj);
    }

    @Override
    public void deleteDividegroups(Long id) {
        // 校验存在
        validateDividegroupsExists(id);
        // 删除
        dividegroupsMapper.deleteById(id);
    }

    private void validateDividegroupsExists(Long id) {
        if (dividegroupsMapper.selectById(id) == null) {
            throw exception(DIVIDEGROUPS_NOT_EXISTS);
        }
    }

    @Override
    public DividegroupsDO getDividegroups(Long id) {
        return dividegroupsMapper.selectById(id);
    }

    @Override
    public List<DividegroupsDO> getDividegroupsList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return dividegroupsMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DividegroupsDO> getDividegroupsPage(DividegroupsPageReqVO pageReqVO) {
        return dividegroupsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DividegroupsDO> getDividegroupsList(DividegroupsExportReqVO exportReqVO) {
        return dividegroupsMapper.selectList(exportReqVO);
    }

}
